package com.bfly.mall.product.service.impl;

import com.bfly.core.base.service.BaseServiceImpl;
import com.bfly.core.enums.SysError;
import com.bfly.core.exception.ServiceResponseException;
import com.bfly.mall.product.dao.ProductSecKillMapper;
import com.bfly.mall.product.dto.ProductSecKillFormDTO;
import com.bfly.mall.product.entity.ProductSecKill;
import com.bfly.mall.product.entity.ProductSku;
import com.bfly.mall.product.enums.ProductActivityStatusEnum;
import com.bfly.mall.product.service.IProductSecKillService;
import com.bfly.mall.product.service.IProductSkuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 长沙商脉科技有限公司
 * @date 2021/2/2 14:13
 */
@Service
public class ProductSecKillServiceImpl extends BaseServiceImpl<ProductSecKill, Integer> implements IProductSecKillService {

    @Autowired
    private IProductSkuService productSkuService;
    @Autowired
    private ProductSecKillMapper productSecKillMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void incrementProductSales(int id, int setup) {
        ProductSecKill secKill = get(id);
        secKill.setUpdateDate(new Date());
        secKill.setSales(secKill.getSales() + setup);
        super.edit(secKill);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(ProductSecKillFormDTO formDTO) {
        ProductSecKill secKill = get(formDTO.getId());
        if (secKill == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "不存在的秒杀产品!");
        }
        ProductSku sku = productSkuService.getSku(secKill.getProductId(), secKill.getSn());
        if (formDTO.getTotal() > sku.getStock()) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "秒杀产品总数量需小于主产品库存量!");
        }
        checkActivityTime(formDTO, secKill);
        secKill.setSeq(formDTO.getSeq());
        secKill.setEnabled(formDTO.isEnabled());
        secKill.setLogo(formDTO.getLogo());
        secKill.setName(formDTO.getName());
        secKill.setPrice(formDTO.getPrice());
        secKill.setSales(formDTO.getSales());
        secKill.setTotal(formDTO.getTotal());
        secKill.setLimits(formDTO.getLimits());
        secKill.setRules(formDTO.getRules());
        secKill.setStartTime(formDTO.getStartTime());
        secKill.setEndTime(formDTO.getEndTime());
        secKill.setUpdateDate(new Date());
        super.edit(secKill);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(ProductSecKillFormDTO formDTO) {
        ProductSku sku = productSkuService.getSku(formDTO.getProductId(), formDTO.getSn());
        if (sku == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "不存在的产品规格!");
        }
        if (formDTO.getTotal() > sku.getStock()) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "秒杀产品总数量需小于主产品库存量!");
        }
        ProductSecKill secKill = new ProductSecKill();
        checkActivityTime(formDTO, secKill);
        BeanUtils.copyProperties(formDTO, secKill);

        secKill.setCreateDate(new Date());
        super.save(secKill);
    }

    /**
     * 活动时间判断
     *
     * @author 长沙商脉科技有限公司
     * @date 2021/2/16 13:55
     */
    private void checkActivityTime(ProductSecKillFormDTO formDTO, ProductSecKill secKill) {
        if (formDTO.getEndTime().before(formDTO.getStartTime())) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "结束时间不能小于开始时间!");
        }
        Date now = new Date();
        if (formDTO.getEndTime().before(now)) {
            secKill.setStatus(ProductActivityStatusEnum.COMPLETED.getId());
        } else if (formDTO.getStartTime().after(now)) {
            secKill.setStatus(ProductActivityStatusEnum.UN_START.getId());
        } else {
            secKill.setStatus(ProductActivityStatusEnum.STARTED.getId());
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editCompleteSecKill() {
        productSecKillMapper.editCompleteSecKill(ProductActivityStatusEnum.COMPLETED.getId());
    }
}